//
//  SecondRegisterViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/24.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SecondRegisterOrResetViewController: AccountBaseViewController {

    @IBOutlet weak var titleLabelEx: UILabel!
    
    @IBOutlet weak var timerBtn: TimerButton!
    
    @IBOutlet weak var inputCodeView: VerifyCodeInputView!
    
    @IBOutlet weak var sureBtn: SureButton!
    
    var type: Int = 0 ///0 手机注册 1 邮箱注册, 2 手机找回密码,3 邮箱找回密码
    
    
    var viewModel:RegisterOrResetViewModel?

    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    func initUI() {
        
        var titleEx: String
        if type == 0 {
            self.title = kText("手机注册")
            titleEx = String(format: kText("短信已发送至%@,请注意查收"), "----")
        }else if type == 1 {
            self.title = kText("邮箱注册")
            titleEx = String(format: kText("邮件已发送至%@,请注意查收"), "----")
        }else if type == 2 {
            self.title = kText("手机找回密码")
            titleEx = String(format: kText("短信已发送至%@,请注意查收"), "----")
        }else {
            self.title = kText("邮箱找回密码")
            titleEx = String(format: kText("邮件已发送至%@,请注意查收"), "----")
        }
      
        if let viewModel = self.viewModel {
            if type == 0 || type == 2 {
                var mobile = viewModel.mobile
                if mobile.count >= 7 {
                    let startIndex = mobile.index(mobile.startIndex, offsetBy: 3)
                    let endIndex = mobile.index(startIndex, offsetBy: 4)
                    mobile.replaceSubrange(startIndex..<endIndex, with: "****")
                }
                titleEx = String(format: kText("短信已发送至%@,请注意查收"), mobile)
            }else {
                var email = viewModel.email
                if email.count >= 7 {
                    let startIndex = email.index(email.startIndex, offsetBy: 3)
                    let endIndex = email.index(startIndex, offsetBy: 4)
                    email.replaceSubrange(startIndex..<endIndex, with: "****")
                }
                titleEx = String(format: kText("邮件已发送至%@,请注意查收"), email)
            }
        }
        
        self.titleLabelEx.text = titleEx
        self.timerBtn.timerState = .active
        self.sureBtn.isEnabled = false
        self.sureBtn.setTitle(kText("下一步"), for: .normal)
//        inputCodeView.isSecurity = true
    
        inputCodeView.verifyCodeChange = {[weak self](text)in
            if text.count == 6 {
                self?.sureBtn.isEnabled = true
                self?.gotoNext()
            }else {
                self?.sureBtn.isEnabled = false
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        initData()
    }
    
    func initData() {
        viewModel?.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_mobileCode {
                if isSuccess {
                 
                    
                }else {
                    if let msg = errorMsg {
                       CYMBShow.showMessage(msg)
                    }
                }
            }else if state == ResponseState.mine_emailCode {
                if isSuccess {
                   
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }else if state == ResponseState.mine_checkMobileCode {
                if isSuccess {
                    let vc = ThirdRegisterOrResetViewController()
                    vc.type = (self?.type)!
                    vc.viewModel = self?.viewModel
                    self?.navigationController?.pushViewController(vc, animated: true)
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }else if state == ResponseState.mine_checkEmailCode {
                if isSuccess {
                    let vc = ThirdRegisterOrResetViewController()
                    vc.type = (self?.type)!
                    vc.viewModel = self?.viewModel
                    self?.navigationController?.pushViewController(vc, animated: true)
                }else {
                    if let msg = errorMsg {
                         CYMBShow.showMessage(msg)
                    }
                }
            }
        }
    }

    @IBAction func reGetCode(_ sender: TimerButton) {
        dPrint("定时器点击")
        sender.timerState = .active
        if self.type == 0 || self.type == 2  {
            self.viewModel?.sendMobileCode()
        }else if self.type == 1 || self.type == 3 {
            self.viewModel?.sendEmailCode()
        }
    }
    
    func gotoNext() {
        if self.type == 0 || self.type == 2 {
            self.viewModel?.checkMobilCode(smsCode: inputCodeView.text!)
        }else {
            self.viewModel?.checkEmailCode(smsCode: inputCodeView.text!)
        }
    }
    @IBAction func next(_ sender: SureButton) {
      self.view.endEditing(true)
       gotoNext()
    }
    

}
